package com.pmsystem.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.Room;
import com.pmsystem.pojo.RoomShow;
import com.pmsystem.pojo.User;
import com.pmsystem.service.RoomService;
import com.pmsystem.service.UserService;

/**
 * 
* <p>Title: RoomController</p>  
* <p>Description:处理房间信息的控制器 </p>  
* @author 树昌  
* @date 2019年1月22日
 */
@Controller
@RequestMapping("Information/")
public class RoomController {

	@Autowired
	RoomService roomService;
	@Autowired
	UserService userService;
	
	
	/**
	 * 
	 * <p>Title: addBuilding</p>  
	 * <p>Description:添加一条房间的记录 </p>  
	 * @author 树昌
	 * @param room
	 * @return
	 */
	@RequestMapping(value="addroom",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addRoom(Room room) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = roomService.addRoom(room);
		if(row) {
			msg = "success";
		}else {
			msg = "success";
		}
		map.put("msg", msg);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: showRoom</p>  
	 * <p>Description:显示所有的房间记录的控制器 </p>  
	 * @author 树昌
	 * @return 不做跳转
	 */
	@RequestMapping(value="showroom",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showRoom(String keyword,Integer startRow,Integer pageSize,Integer RID,Integer UID) {
		
		Map<String, Object> map = new HashMap<>();
		List<RoomShow> listroom = roomService.showRoom(keyword, startRow, pageSize,RID,UID);
		map.put("listroom", listroom);
		int size = listroom.size();
		map.put("size", size);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: showRoomByRoomId</p>  
	 * <p>Description:根据房间ID查询一条房间信息 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value="showRoomByRoomId",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showRoomByRoomId(int id){
		Map<String, Object> map = new HashMap<>();
		Room room = roomService.showRoomByRoomID(id);
		map.put("room", room);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: showRoomByUserId</p>  
	 * <p>Description:根据房间ID查询一条房间的记录 </p>  
	 * @author 树昌
	 * @param request
	 * @return
	 */
	@RequestMapping(value="showRoomByUserId",method=RequestMethod.POST)
	@ResponseBody
	public List<RoomShow> showRoomByUserId(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		Integer UID =null;
		if(user!=null) {
			UID = user.getUserId();
		}
		return roomService.showRoom(null, null, null,null,UID);
	}

	
	/**
	 * 
	 * <p>Title: deleteRoom</p>  
	 * <p>Description:删除一条房间的记录 </p>  
	 * @author 树昌
	 * @param room
	 * 
	 */
	@RequestMapping(value="deleteroom",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteRoom(int id) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		Room room=new Room();
		room.setRoomId(id);
		boolean row = roomService.deleteRoom(room);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteAllRoom</p>  
	 * <p>Description:批量删除房间记录 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value="deleteAllrooms",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteAllRoom(int[] id) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		List<Integer> listID = new ArrayList<>();
		for (int i : id) {
			listID.add(i);
		}
		
		boolean row = roomService.deleteAllRoom(listID);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	
	/**
	 * 
	 * <p>Title: updateRoom</p>  
	 * <p>Description:根据房间ID修改一条房间的信息 </p>  
	 * @author 树昌
	 * @param room
	 * @return 跳转到查询所有房间信息的页面
	 */
	@RequestMapping(value="updateroom",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateRoom(Room room) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = roomService.updateRoom(room);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg",msg);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: getUserInfoByUserName</p>  
	 * <p>Description:根据用户名获取用户信息 </p>  
	 * @author 树昌
	 * @param userName
	 * @return
	 */
	@RequestMapping(value="getUserInfoByUserName",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> getUserInfoByUserName(String userName){
		
		Map<String,Object> map = new HashMap<>();
		User user = userService.getUserInfoByUserName(userName);
		map.put("user", user);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: getUserInfoByUserID</p>  
	 * <p>Description:根据用户ID获取用户信息 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@RequestMapping(value="getUserInfoByUserID",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> getUserInfoByUserID(Integer id){
		
		Map<String,Object> map = new HashMap<>();
		User user = userService.getUserInfoByUserID(id);
		map.put("user", user);
		return map;
	}
}
